package com.zsyh.jiujiu.adepter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.MyGoodBean;

import java.util.List;

/**
 * 描    述：
 * 创建日期：2017/7/17 16:01
 * 作    者：Chengfu
 * 邮    箱：
 * 备    注：
 */
public class SingleAdapter extends RecyclerView.Adapter {

    private List<MyGoodBean.DataBean> datas;

    private int selected = -1;
    Context context;
    public SingleAdapter(List<MyGoodBean.DataBean> datas, Context context) {
        this.datas = datas;
        this.context=context;
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public void setSelection(int position) {
        this.selected = position;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
        return new SingleViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof SingleViewHolder) {
            final SingleViewHolder viewHolder = (SingleViewHolder) holder;
            String name = datas.get(position).getGoodsTitle();
            viewHolder.name.setText(name);
            viewHolder.price.setText(datas.get(position).getGoodsPrice()+"");
            viewHolder.store.setText("库存："+datas.get(position).getStockCount()+datas.get(position).getGoodsUnit());
            String[] split = datas.get(position).getGoodsPic().split("\\|");
            Glide.with(context).load(split[0]).into(viewHolder.logo);
            if (selected == position) {
                viewHolder.mCheckBox.setChecked(true);

            } else {
                viewHolder.mCheckBox.setChecked(false);
            }

            if (mOnItemClickLitener != null) {
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnItemClickLitener.onItemClick(viewHolder.itemView, viewHolder.getAdapterPosition());
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class SingleViewHolder extends RecyclerView.ViewHolder {
        CheckBox mCheckBox;
        TextView name;
        TextView price;
        TextView store;
        ImageView logo;
        public SingleViewHolder(View itemView) {
            super(itemView);
            mCheckBox = (CheckBox) itemView.findViewById(R.id.checkbox);
            name = (TextView) itemView.findViewById(R.id.huanrugoodname);
            price = (TextView) itemView.findViewById(R.id.money);
            store = (TextView) itemView.findViewById(R.id.kucun);
            logo = (ImageView) itemView.findViewById(R.id.huanrulogo);

        }
    }

}
